# !/user/bin/env python3
# -*- encoding: utf-8 -*-
'''
@File    :   66law.py
@Time    :   2020/12/14 21:40:17
@Author  :   cecil-y 
@Version :   1.0
@Desc    :   None
华律
'''

# here put the import lib

from requests_html import HTMLSession
import xlwt

session = None
data = []


def create_session():
    global session
    session = HTMLSession()
    # session.cookies.set('AntiLeech', '2676165397')
    session.headers.update({
        "User-Agent":
        "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:80.0) Gecko/20100101 Firefox/80.0"
    })


def get_html():
    for i in range(10):
        r = session.get(
            url='https://www.66law.cn/guangzhou/zhishichanquan/page_' + str(i) +
            '.aspx')
        global data
        for item in r.html.find(".lr-item.dc-el"):
            name = item.find(".name")[0].text
            office = []
            for o in item.find('.office.ect span'):
                office.append(o.text)
            phone = item.find('.tel.din-m')[0].text
            detail = item.find('.tx')[0].text
            data.append({
                'name': name,
                'detail': detail,
                'phone': phone,
                'office': ' '.join(office)
            })


def export_excel():
    # 创建工作簿
    workbook = xlwt.Workbook(encoding='utf-8')
    # 创建sheet
    data_sheet = workbook.add_sheet('华律')

    global data
    for i in range(len(data)):
        data_sheet.write(i, 0, data[i]['name'])
        data_sheet.write(i, 1, data[i]['detail'])
        data_sheet.write(i, 2, data[i]['phone'])
        data_sheet.write(i, 3, data[i]['office'])
    workbook.save('华律' + '.xls')
    pass


if __name__ == '__main__':
    create_session()
    get_html()
    export_excel()

    pass